package com.ruoyi.system.third.util;

import cn.hutool.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;

/**
 * 签名工具
 */
public class ApiDemoSignUtil {

	public static final Logger LOGGER = LogManager.getLogger(ApiDemoSignUtil.class);

	/** 签名校验 */
	public static String apiSign(SortedMap<String, Object> sort, String partnerKey) {
		String sign = "";
		StringBuilder signBuilder = new StringBuilder();
		StringBuilder blankBuilder = new StringBuilder();
		if (StringUtils.isNotEmpty(partnerKey) && sort != null && !sort.isEmpty()) {
			for (String key : sort.keySet()) {
				if (!"sign".equals(key) && !"imgData".equals(key) && sort.get(key) != null) {
					signBuilder.append(key).append("=").append(sort.get(key)).append("&");
				}
				if (StringUtils.isBlank(sort.get(key).toString())) {
					blankBuilder.append(sort.get(key));
				}
			}
		}
		if (signBuilder.length() > 0) {
			signBuilder.append("key=").append(partnerKey);
			sign = MD5DemoUtil.getMD5String(signBuilder.toString());
		}
		LOGGER.info("API签名参数:" + signBuilder + "," + " 签名:" + sign);
		if (blankBuilder.length() > 0) {
			LOGGER.info("blankBuilder参数:" + signBuilder + "," + " 签名:" + sign + "--" + blankBuilder.toString());
		}
		return sign;
	}


	/** 签名校验 */
	public static String apiSign(Map<String, String> map, String partnerKey) {
		List<String> list = new ArrayList<>();
		// 将价值对组成 key=value,value的格式 比存入list
		for (String key : map.keySet()) {
			if (!"sign".equals(key) && !"imgData".equals(key) && map.get(key) != null) {
				list.add(key + "=" + map.get(key));
			}
		}
		// 将list转成String数组并按照字段顺序排序
		String[] strs = list.toArray(new String[0]);
		Arrays.sort(strs);

		// 将字符串数组用“&”拼接成字符串并加上partnerKey
		StringBuilder sb = new StringBuilder();
        for (String str : strs) {
            sb.append(str).append("&");
        }
		sb.append("key=").append(partnerKey);
		// 返回加密结果
		String sign = MD5DemoUtil.getMD5String(sb.toString());
		LOGGER.info("API签名参数:" + sb + "," + " 签名:" + sign);
		return sign;
	}

	/** 签名校验 */
	public static String apiSign(JSONObject jsonObject, String partnerKey) {
		List<String> list = new ArrayList<>();
		// 将价值对组成 key=value,value的格式 比存入list
		for (String key : jsonObject.keySet()) {
			if (!"sign".equals(key) && !"imgData".equals(key) && jsonObject.get(key) != null) {
				list.add(key + "=" + jsonObject.get(key));
			}
		}
		// 将list转成String数组并按照字段顺序排序
		String[] strs = list.toArray(new String[0]);
		Arrays.sort(strs);

		// 将字符串数组用“&”拼接成字符串并加上partnerKey
		StringBuilder sb = new StringBuilder();
        for (String str : strs) {
            sb.append(str).append("&");
        }
		sb.append("key=").append(partnerKey);
		// 返回加密结果
		String sign = MD5DemoUtil.getMD5String(sb.toString());
		LOGGER.info("API签名参数:" + sb + "," + " 签名:" + sign);
		return sign;
	}

	private static final String[] NUMBER = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
	/** 获取八位随机数 */
	public static String getRandom() {
		StringBuilder shortBuffer = new StringBuilder();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(NUMBER[x % 0x0a]);
		}
		return shortBuffer.toString();

	}
}
